package com.example.test_bluetooth;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Set;

import android.app.*;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.*;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;


public class ChoixServeur extends ListActivity  {


	private TextView selection;
	private static String serveurchoisi = "Aucun serveur pour le moment";
	private ArrayList<String> liste_apparie = new ArrayList<String>(42);
	private Set<BluetoothDevice> serv;
	private static int pos=0;
	private BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();	
	private static final String TAG = "Choixserveur";
	Button validerserveur ;
	private static ArrayList<BluetoothDevice> deviceList = new ArrayList<BluetoothDevice>(); 
	private ArrayAdapter<String> aa; 


	public void remplirListe(BluetoothAdapter bluetooth){
		serv = bluetooth.getBondedDevices();

		

		if(serv == null)
			Toast.makeText(ChoixServeur.this, "probleme c null", Toast.LENGTH_SHORT).show();
		for(BluetoothDevice bd: serv){
			//Toast.makeText(ChoixServeur.this, "Device = " + bd.getName(), Toast.LENGTH_SHORT).show();
			//if(!appartient(deviceList,bd)){
				liste_apparie.add(bd.getName());
				deviceList.add(bd);
			//}
			deviceList.add(bd);
			Log.d("toto"," remplir liste devicelist[0]= " + deviceList.get(0));
		}
		/*

		if(deviceList.isEmpty())
			Toast.makeText(ChoixServeur.this, "vide 2", Toast.LENGTH_SHORT).show();

		for(BluetoothDevice bd : getDeviceList())
		{

			liste_apparie.add(bd.getName());
		}

		 */
		
		bluetooth.startDiscovery();
		IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
		registerReceiver(discoveryResult, filter);
	}

	private boolean appartient(ArrayList<BluetoothDevice> deviceList2,
			BluetoothDevice bd) {
		// TODO Auto-generated method stub
		for(BluetoothDevice bt : deviceList2){
			if(bt.getName().compareTo(bd.getName())==0){
				Log.d("ChoixServeur", "bt.getName : " + bt.getName() + "bd.getName : "+ bd.getName());
				return true;
			}
		}
		return false;

	}

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choixliste);





		remplirListe(bluetooth);
		aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, getListe_apparie());
		setListAdapter(aa);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		if(pos == 0)
			getListView().setItemChecked(0, true);
		else
			getListView().setItemChecked(pos, true);

		//getListView().setItemChecked(position, true);
		selection = (TextView) findViewById(R.id.selection);
		selection.setText("Le serveur actuel est : "+ serveurchoisi);

		/*refresh.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, getListe_apparie()));
			}			
		}); */

	}


		@Override
	public void onDestroy() {
		super.onDestroy();
		unregisterReceiver(discoveryResult);
	}

	public static ArrayList<BluetoothDevice> getDeviceList() {
		return deviceList;
	}

	public void setDeviceList(ArrayList<BluetoothDevice> deviceList) {
		this.deviceList = deviceList;
	}

	BroadcastReceiver discoveryResult = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent){
			String remoteDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
			BluetoothDevice remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

			//deviceList.add(remoteDevice);
						
			if(appartient(getDeviceList(),remoteDevice) == false)
			{
				deviceList.add(remoteDevice);
				aa.add(remoteDeviceName);
			}
			Log.d("toto","Découverte " + remoteDeviceName);
			Log.d("toto","devicelist[0]= " + deviceList.get(0));
			if(deviceList.isEmpty())
				Toast.makeText(ChoixServeur.this, "vide 2", Toast.LENGTH_SHORT).show();
		}
	};

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		selection.setText("Vous avez choisi : "+liste_apparie.get(position));
		serveurchoisi = liste_apparie.get(position);
		pos = position;
	} 

	public static String getServeurChoisi(){
		return serveurchoisi;
	}

	public ArrayList<String> getListe_apparie()
	{
		return liste_apparie;
	}


}


